function [ boltzman_individuals ] = boltzman( individuals, n_of_selection )

% BOLTZMAN
%   Este metodo realiza la seleccion a traves del metodo de Boltzman.
%   Se recibe el conjunto de individuos y la cantidad a seleccionar a
%   partir de los mismos.

global TEMP;
global GENERATION;

T = TEMP/(GENERATION+1);

fitness_array = zeros(1, length(individuals));
for i = 1:length(individuals)
   fitness_array(i) = fitness(individuals{i});
end

average_fitness = sum(exp(fitness_array./T));
relative_fitness = zeros(1, length(individuals));
accumulative_fitness = zeros(1, length(individuals));

for i = 1:length(individuals)
    relative_fitness(i) = exp(fitness_array(i)/T) / average_fitness;
    if i > 1
        accumulative_fitness(i) = accumulative_fitness(i-1) + relative_fitness(i);
    else
        accumulative_fitness(i) = relative_fitness(i);
    end
end

%fprintf('Desvio Estandar: %f Temperatura : %f\n', std(relative_fitness), T)


r = rand(1, n_of_selection);
boltzman_individuals = cell(1, n_of_selection);

for i = 1:n_of_selection
    j = 1;
    while r(i) > accumulative_fitness(j)
        j = j + 1;
    end
    boltzman_individuals{i} = individuals{j};
end

end

